package com.example.teamtribe.main.mine.info;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.common.model.UserModel;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import java.io.File;

public class MyInfoPresenter implements MyInfoContract.Presenter {

    private MyInfoContract.View view;
    private MyInfoModel model;

    public MyInfoPresenter(MyInfoContract.View view) {
        this.view = view;
        model = new MyInfoModel();
    }

    @Override
    public void update(File head, String nickname, String portrait, int isPublic) {
        if (TextUtils.isEmpty(nickname)) {
            view.showToast("昵称不能为空");
            return;
        }
        view.showProgress();
        if (head == null) {
            updateInfo(nickname, portrait, isPublic);
        } else {
            uploadHead(head, nickname, isPublic);
        }
    }

    private void uploadHead(File head, final String nickname, final int isPublic) {
        model.uploadHead(Utility.getToken(), head, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                updateInfo(nickname, bundle.getString("fileName"), isPublic);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    private void updateInfo(String nickname, String portrait, int isPublic) {
        model.updateInfo(Utility.getToken(), nickname, portrait, isPublic, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                UserModel.getInstance().getUser(Utility.getToken(), Utility.getUser().getRealId(), new FileModel.FileBack() {
                    @Override
                    public void loadSuccess(String fileName) {
                        view.next();
                    }

                    @Override
                    public void success(@Nullable Bundle bundle) {
                        User user =  bundle.getParcelable("user");
                        user.setNo("201616080311");
                        user.setName("刘峰");
                        user.setGrade(2016);
                        user.setMajor("软件工程");
                        user.setSchoolId(1);
                        user.setBirthday("1998-10-23");
                        user.setGender(1);
                        user.setAcademy("计算机与通信工程学院");
                        user.setCla(3);
                        Utility.setUser(user);
                    }

                    @Override
                    public void failure(String meg) {
                        view.hideProgress();
                        view.showToast(meg);
                    }
                });
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }
}